﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Box2D.XNA;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Elementum
{
    public class Item: Actor
    {
        protected  bool _IsCarring;
        protected  bool _isUsed;
        protected float _speed = 3.4f;
        public bool IsCarring {
            get { return _IsCarring; }
            set { 
                _IsCarring = value;
                if (StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable.ContainsKey(ItemID))
                {
                    StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable[ItemID]._IsCarring = value;
                }
            }
        }
        public bool IsUsed {
            get { return _isUsed; }
            set { _isUsed = value;
            if (StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable.ContainsKey(ItemID))
            {
                StageManager.Save.StageStateTable[StageManager.CurrentStateID].ItemStateTable[ItemID]._isUsed = value;
            }
            }
        } 

        public virtual void ThrowOutItem(){}

        public Actor _owner;

        public string ItemID
        {
            get;
            set;
        }

        public Item(World world,Stage stage): base(world,stage)
        {
            _IsCarring = false;
            _isUsed = false;
        }

        public Item(String Id,World world, Stage stage)
            : base(world, stage)
        {
            if (GameStage.player != null)
                _owner = GameStage.player;
            ItemID = Id;
            _IsCarring = false;
            _isUsed = false;
        }

        public override void CreatBody(World world)
        {
            this._world = world;
            BodyDef bodyDef = new BodyDef();
            _body = world.CreateBody(bodyDef);
            _body.Owner = this;
            _body._userData = "Item";
            _body._type = BodyType.Dynamic;
        }

        protected override void ConfigFilter()
        {
            //base.ConfigFilter();
            _filter.categoryBits = ItemCategory;
            _filter.maskBits = ItemMask;
        }

        public override void Update(int delta)
        {
            base.Update(delta);
        }

        public override void BeginContactAction(Contact contact, Body contactBody, float normal)
        {
            base.BeginContactAction(contact, contactBody, normal);
            if (!IsUsed&&(contactBody.GetUserData() == "player" || contactBody.GetUserData() == "monster") && !_IsCarring)
            {
                _owner = (Actor)contactBody.Owner;
                _filter.maskBits = 0x00;
                _fixture.SetFilterData(ref _filter);
                _IsCarring = true;
            }
        }

        public override void EndContactAction(Contact contact, Body contactBody, float normal)
        {
            base.EndContactAction(contact, contactBody, normal);
        }

    }
}
